package com.sinping.qh.api.app.version.v1.noticebook;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.app.noticebook.NoticeBookByAppDTO;
import com.sinping.qh.dto.app.records.HiddendangerByAppDTO;
import com.sinping.qh.dto.app.records.HiddendangerNoticeBookByAppDto;
import com.sinping.qh.dto.app.records.HiddendangerNoticeBookGroupByAppDto;
import com.sinping.qh.dto.mongo.auth.UserSessionDTO;
import com.sinping.qh.repository.mongo.records.HiddenDangerRecordRepository;
import com.sinping.qh.service.app.noticebook.INoticeBookServiceApp;
import com.sinping.qh.service.auth.ISessionService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jiahuaiqing
 * @create 2018/6/9
 */
@Api(value = "app通知书", description = "app通知书API")
@RestController
@RequestMapping(value = Const.APP_URI + "/noticebook", name = "通知书")
public class NoticeBookByAppEndPoint extends BaseAppEndPoint {

    @Autowired
    private INoticeBookServiceApp iNoticeBookServiceApp;

    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    ISessionService iSessionService;


    @ApiOperation(httpMethod = "GET", value = "通知书列表",notes = "条件中只能添加：notice_receive_unit_name;<BR>notice_send_date_start和notice_send_date_end时间格式为：2018-07-01 ")
    @RequestMapping(name = "通知书列表", method = RequestMethod.GET)
    public ResDto getList(@ApiParam(name = "msg", value = "{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\"" +
            ", \"condition\":{\"notice_receive_unit_name\":\"123\",\"notice_send_date_start\":\"2018-07-01\"" +
            ",\"notice_send_date_end\":\"2018-07-01\"}}")
                          @RequestParam(required = true) String msg) {

        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        logger.info("入参 {}", msg);
        FrontPage<NoticeBookByAppDTO> frontPage = JsonMapper.INSTANCE.fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage, ConstEum.PARAM_ERROR);

        UserSessionDTO userSessionDto = getUserSessionDto();

        //查询数据
        frontPage = iNoticeBookServiceApp.findPage(frontPage, userSessionDto);

        checkNull(frontPage, ConstEum.FAIL);

        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        resDto.setMsg("操作成功");
        return resDto;

    }


    @Autowired
    HiddenDangerRecordRepository hiddenDangerRecordRepository;

    /**
     * 通知书中隐患记录,按楼宇实验室分组的列表
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "通知书中的楼宇实验室分组的列表", notes = "hidden_danger_ids是隐患id的集合")
    @RequestMapping(name = "通知书中的楼宇实验室分组的列表", method = RequestMethod.GET, value = "/groupbybyhiddendangerids")
    public ResDto getGroupByHiddenDangerIds(@ApiParam(name = "msg", value = "{\"hidden_danger_ids\":[\"XXXXX\",\"XXXXX\"]}",required = true)
                             @RequestParam(required = true) String msg) {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);
        NoticeBookByAppDTO noticeBookByAppDTO = JsonMapper.defaultMapper().fromJson(msg, NoticeBookByAppDTO.class);

        //查询时检查查询条件格式
        checkNull(noticeBookByAppDTO, ConstEum.PARAM_ERROR);

        List<String> hiddenDangerIds = noticeBookByAppDTO.getHiddenDangerIds();

        if (null == hiddenDangerIds || hiddenDangerIds.size() == 0) {
            return new ResDto(ConstEum.PARAM_ERROR,"隐患id不能空");
        }

        UserSessionDTO userSessionDto = getUserSessionDto();

        //根据隐患记录的id集合查询数据
        List<HiddendangerNoticeBookGroupByAppDto> hiddendangerNoticeBookGroupByAppDto = iNoticeBookServiceApp.findUnitByHiddenDangerIds(hiddenDangerIds, userSessionDto);

        checkNull(hiddendangerNoticeBookGroupByAppDto, ConstEum.FAIL);

        resDto.setDatas(JsonMapper.defaultMapper().toJson(hiddendangerNoticeBookGroupByAppDto));
        resDto.setMsg("操作成功");
        return resDto;
    }


    /**
     * 获取隐患记录列表(通知书中,通过楼宇实验室分组的列表)
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取隐患记录列表(通知书中)", notes = "hidden_danger_ids是隐患id的集合 必填<BR>综合隐患 type:0 ,check_building_id 必填<BR>实验室隐患：type:1 ,check_lab_id 必填")
    @RequestMapping(name = "获取隐患记录列表(通知书中)", method = RequestMethod.GET, value = "/hiddendangerrecords")
    public ResDto getHiddenDangerRecords(@ApiParam(name = "msg", value = "{\"hidden_danger_ids\":[\"XXXXX\",\"XXXXX\"],\"check_lab_id\":\"XXXXX\",\"type\":\"XXXXX\"}",required = true)
                             @RequestParam(required = true) String msg) {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);
        HiddendangerNoticeBookByAppDto hiddendangerNoticeBookByAppDto = JsonMapper.defaultMapper().fromJson(msg, HiddendangerNoticeBookByAppDto.class);

        //查询时检查查询条件格式
        checkNull(hiddendangerNoticeBookByAppDto, ConstEum.PARAM_ERROR);

        List<String> hiddenDangerIds = hiddendangerNoticeBookByAppDto.getHiddenDangerIds();

        if (null == hiddenDangerIds || hiddenDangerIds.size() == 0) {
            return new ResDto(ConstEum.PARAM_ERROR,"隐患id不能空");
        }

        if (!StringUtils.isNotBlank(hiddendangerNoticeBookByAppDto.getType())) {
            return new ResDto(ConstEum.PARAM_ERROR,"type不能空");
        }

        if (hiddendangerNoticeBookByAppDto.getType().equals("0")&&!StringUtils.isNotBlank(hiddendangerNoticeBookByAppDto.getCheckBuildingId())) {
            return new ResDto(ConstEum.PARAM_ERROR,"楼宇id不能空");
        }

        if (hiddendangerNoticeBookByAppDto.getType().equals("1")&&!StringUtils.isNotBlank(hiddendangerNoticeBookByAppDto.getCheckLabId())) {
            return new ResDto(ConstEum.PARAM_ERROR,"实验室id不能空");
        }

        UserSessionDTO userSessionDto = getUserSessionDto();

        //根据隐患记录的id集合查询数据
        List<HiddendangerByAppDTO> hiddendangerNoticeBookGroupByAppDto = iNoticeBookServiceApp.findHiddenDangerRecords(hiddendangerNoticeBookByAppDto, userSessionDto);

        checkNull(hiddendangerNoticeBookGroupByAppDto, ConstEum.FAIL);

        resDto.setDatas(JsonMapper.defaultMapper().toJson(hiddendangerNoticeBookGroupByAppDto));
        resDto.setMsg("操作成功");
        return resDto;
    }
}
